package com.vm.db;

import java.util.ArrayList;
import java.util.List;
import com.vm.model.UpgradeSoft;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年4月3日 上午9:39:48]
 * @Version: [v1.0]
 * 
 */
public class UpgradeSoftDao {

	private static final String TAG = "UpgradeSoftDao";

	// SQLiteDatabase db = null;

	// public UpgradeSoftDao(Context context) {
	// if (DBOpenHelper.db == null) {
	// new DBOpenHelper(context);
	//
	// }
	// }

	public synchronized void insert(UpgradeSoft upgradeSoft) {
		Log.d(TAG, "insert");
		try {

			DBOpenHelper.db
					.execSQL(
							"INSERT INTO upgradeSoft(id,softId,name,packageName,currentVersion,newVersion,size) VALUES(?, ?,?,?,?,?,?)",
							new Object[] { upgradeSoft.getId(),
									upgradeSoft.getSoftId(),
									upgradeSoft.getName(),
									upgradeSoft.getPackageName(),
									upgradeSoft.getCurrentVersion(),
									upgradeSoft.getNewVersion(),
									upgradeSoft.getSize() });
		} catch (SQLException e) {
			Log.d(TAG, "insert---" + e.getMessage());
		}
	}

	public void delete(String packageName) {
		Log.d(TAG, "delete");

		try {
			if (packageName != null) {
				DBOpenHelper.db.execSQL(
						"DELETE FROM upgradeSoft where packageName=?",
						new Object[] { packageName });
			}
		} catch (SQLException e) {
			Log.d(TAG, "delete---" + e.getMessage());
		}
	}

	public void deleteByPackageName(String packageName) {
		Log.d(TAG, "delete");

		try {
			if (packageName != null) {
				DBOpenHelper.db.execSQL(
						"DELETE FROM upgradeSoft where packageName=?",
						new Object[] { packageName });
			}
		} catch (SQLException e) {
			Log.d(TAG, "delete---" + e.getMessage());
		}
	}

	/**
	 * 如果当前版本和newVersion一致,将其更新为没有更新状态
	 * 
	 * @param packageName
	 */
	public synchronized void update(String packageName) {
		Log.d(TAG, "update");
		try {
			if (packageName != null) {
				DBOpenHelper.db
						.execSQL("UPDATE upgradesoft SET softId=null,newVersion=null,size=0 WHERE packageName='"
								+ packageName + "'");
			}
		} catch (SQLException e) {
			Log.d(TAG, "update---" + e.getMessage());
		}
	}

	/**
	 * 是否存在,
	 * 
	 * @param packageName
	 * @return
	 */
	public boolean ifExists(String packageName) {
		Log.d(TAG, "ifExists");
		Cursor c = null;
		try {
			c = DBOpenHelper.db.rawQuery(
					"SELECT * FROM upgradesoft where packageName=? ",
					new String[] { packageName });
			if (c.moveToNext()) {
				return true;
			}
		} catch (Exception e) {
			Log.d(TAG, "ifExists---" + e.getMessage());
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return false;
	}

	public synchronized UpgradeSoft query(String packageName) {
		Log.d(TAG, "query");
		Cursor c = null;
		UpgradeSoft upgradeSoft = null;
		try {
			c = DBOpenHelper.db.rawQuery(
					"SELECT * FROM upgradesoft where packageName=?",
					new String[] { packageName });
			upgradeSoft = null;
			if (c.moveToNext()) {
				upgradeSoft = new UpgradeSoft();
				upgradeSoft.setId(c.getString(0));
				upgradeSoft.setSoftId(c.getString(1));
				upgradeSoft.setName(c.getString(2));
				upgradeSoft.setPackageName(c.getString(3));
				upgradeSoft.setCurrentVersion(c.getString(4));
				upgradeSoft.setNewVersion(c.getString(5));
				upgradeSoft.setSize(c.getFloat(6));
			}
		} catch (Exception e) {
			Log.d(TAG, "query---" + e.getMessage());
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return upgradeSoft;
	}

	public synchronized List<UpgradeSoft> getUninstall() {
		List<UpgradeSoft> list = new ArrayList<UpgradeSoft>();
		Log.d(TAG, "getUninstall");
		Cursor c = null;

		try {

			c = DBOpenHelper.db.rawQuery("SELECT * FROM upgradeSoft", null);
			while (c.moveToNext()) {
				UpgradeSoft upgradeSoft = new UpgradeSoft();
				upgradeSoft = new UpgradeSoft();
				upgradeSoft.setId(c.getString(0));
				upgradeSoft.setSoftId(c.getString(1));
				upgradeSoft.setName(c.getString(2));
				upgradeSoft.setPackageName(c.getString(3));
				upgradeSoft.setCurrentVersion(c.getString(4));
				upgradeSoft.setNewVersion(c.getString(5));
				upgradeSoft.setSize(c.getFloat(6));
				list.add(upgradeSoft);
			}

		} catch (Exception e) {
			Log.d(TAG, "getUninstall---" + e.getMessage());
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return list;
	}

	public synchronized List<UpgradeSoft> getUpgradeSofts() {
		List<UpgradeSoft> list = new ArrayList<UpgradeSoft>();
		Log.d(TAG, "query");
		Cursor c = null;

		try {
			c = DBOpenHelper.db.rawQuery(
					"SELECT * FROM upgradeSoft where softId is not null", null);
			while (c.moveToNext()) {
				UpgradeSoft upgradeSoft = new UpgradeSoft();
				upgradeSoft = new UpgradeSoft();
				upgradeSoft.setId(c.getString(0));
				upgradeSoft.setSoftId(c.getString(1));
				upgradeSoft.setName(c.getString(2));
				upgradeSoft.setPackageName(c.getString(3));
				upgradeSoft.setCurrentVersion(c.getString(4));
				upgradeSoft.setNewVersion(c.getString(5));
				upgradeSoft.setSize(c.getFloat(6));
				list.add(upgradeSoft);
			}

		} catch (Exception e) {
			Log.d(TAG, "getUpgradeSofts---" + e.getMessage());
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return list;
	}

}
